package com.example.learnspringboot05web.config;

import com.example.learnspringboot05web.component.MyLocaleResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/*
    WebMvcAutoConfigurationAdapter 扩展 springmvc 功能

    WebMvcConfigurerAdapter详解和过时后的替代方案（转载）
    https://blog.csdn.net/qq_47155894/article/details/127863264



    查询 DefaultErrorAttributes	源码，里面有对应的字段

    #SpringBoot-RuntimeException缺少exception和message问题解决方法,  这个问题是由于SpringBoot2.0以上版本，需要在配置文件中指定server的异常对象和异常信息
    # https://blog.csdn.net/qq_40898909/article/details/126346500     https://blog.csdn.net/qq_33879627/article/details/106621563
    # 查看 ErrorProperties 源码 private IncludeAttribute includeMessage = IncludeAttribute.NEVER;  includeException 默认 false
    server.error.include-exception=true
    server.error.include-message=always

 */
//@EnableWebMvc // 这里不能添加这个
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //发送test请求来到success.xml页面
        registry.addViewController("/test").setViewName("success");
    }

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean// 将组件注册在容器中
    public WebMvcConfigurer webMvcConfigurer() {
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
//                registry.addViewController("/").setViewName("login");
//                registry.addViewController("/login.html").setViewName("login");

                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
        };

        return webMvcConfigurer;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //静态资源；  *.css , *.js
//    原文链接：https://blog.csdn.net/sinat_42483341/article/details/104095196
        //静态资源不需要处理，可以正常访问。springboot已经做好了静态资源映射
        //TODO 先注释掉下面的代码
        /*
            演示springboot 处理错误原理 以及 定制错误页面 查看 ErrorMvcAutoConfiguration 源码
            error文件夹里的 状态码.html(错误页面)
            4xx.html 以4开头的状态码, 例如 400
            5xx.html
         */
//        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//                .excludePathPatterns("/","/index.html","login","/login", "/login.html", "/user/login", "/**/*.css", "/**/*.js", "/**/*.png",
//                        "/**/*.jpg","/**/*.jpeg", "/**/*.gif", "/**/fonts/*", "/**/*.svg","/**/*.ico","/**/*.map");
    }


    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}
